<?php 
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

include_once("includes/db.inc");
include_once("includes/functions.inc");

session_start();
include_once("includes/session.inc");

ControleAcces("statbook.php",$login,$pwd,$user_id,session_id());

if(!$user_id)
{
 exit;
}

include ("includes/header.inc");

echo "<CENTER><br>";
$sql = "SELECT id FROM livres" ;	
$result = biblio_db_query($sql);
$total_liv=biblio_db_num_rows($result);
$sql = "SELECT id FROM livres where del =1" ;	
$result = biblio_db_query($sql);
$total_livdel=biblio_db_num_rows($result);
$total_livnodel = $total_liv - $total_livdel;
$sql = "SELECT DISTINCT livre_id FROM prets" ;	
$result = biblio_db_query($sql);
$total_livoui=biblio_db_num_rows($result);
$sql = "SELECT DISTINCT livres.id  FROM livres, prets where livres.id = prets.livre_id" ;	
$result = biblio_db_query($sql);
$total_livno=$total_liv - biblio_db_num_rows($result);
$sql = "SELECT id  FROM prets" ;	
$result = biblio_db_query($sql);
$total_emp=biblio_db_num_rows($result);


if ($total_livoui > 1) {$term1 = "s"; $term2 = "ont"; }
else {$term2 = "a";}
if ($total_livno > 1) {$term3 = "s"; $term4 = "ont"; }
else {$term4 = "a";}
if ($total_livdel > 1) {$term5 = "s"; $term6 = "ont"; }
else {$term6 = "a";}

echo _("Sur <B>$total_liv</B> livres, <B>$total_livdel</B> $term6 quitt&eacute;$term5 la bilioth&egrave;que (perdu$term5, rendu$term5), <B>$total_livoui</B> livre$term1 $term2 &eacute;t&eacute; emprunt&eacute;$term1");
echo _(" et <B>$total_livno</B> livre". $term3 ." n'$term4 jamais &eacute;t&eacute; emprunt&eacute;$term3 <p>Il y a eu <B>$total_emp</B> emprunts.");

echo "<HR><H3>";
echo _("Top 5 des livres les plus lus :");
echo "</H3>";
$sql = "SELECT DISTINCT t1.livre_id, count(t1.livre_id) tot, t2.nom FROM prets as t1 inner join livres as t2 on t2.id=t1.livre_id group by livre_id order by tot desc, nom LIMIT 5" ;	
$result = biblio_db_query($sql);
put_log(8,"top 5 livres les plus lus",$user);
echo "<TABLE BORDER=1><tr><th>";
echo _("Titre");
echo "</th><th>";
echo _("Nbr d'emprunts");
echo "</th><th>";
echo _("R&eacute;partition"); 
echo "</B></TD>";
while ($myrow = biblio_db_fetch_array($result)) 
{
                $nom = $myrow['nom'];
                $tot  = $myrow['tot'];
                $i++;
                if ($i%2 == 1) {$col="WHITE";}
                else {$col="lightyellow";}
                echo "<TR bgcolor=$col><TD>". $nom ."</TD><TD align=right>". $tot ."</TD>";      
                $pct = sprintf("%2d",($tot / $total_emp) * 100);
                echo "<TD><IMG SRC=\"images/leftbar.gif\" height=\"15\" width=\"7\" Alt=\"$nom : $pct%\" border=0>";
                echo"<IMG SRC=\"images/mainbar.gif\" height=\"15\" width=\"". $pct  ."\" Alt=\"$nom : $pct%\" border=0>";
	        echo "<IMG SRC=\"images/rightbar.gif\" height=\"15\" width=\"7\" Alt=\"$nom : $pct%\" border=0> $pct %</TD>";
}
echo "</TABLE><BR>";

echo "<HR><H3>";
echo _("Top 5 des livres emprunt&eacute;s les moins lus :");
echo "</H3>";
$limit = $total_livoui - 5;
if ($limit < 0) $limit=0;
$sql = "SELECT DISTINCT t1.livre_id, count(t1.livre_id) tot, t2.nom FROM prets as t1 inner join livres as t2 on t2.id=t1.livre_id group by livre_id order by tot desc, nom LIMIT $limit,5" ;	
$result = biblio_db_query($sql);
put_log(8,"top 5 livres les moins lus",$user);
echo "<TABLE BORDER=1><tr><th>";
echo _("Titre");
echo "</th><th>";
echo _("Nbr d'emprunts");
echo "</th><th>";
echo _("R&eacute;partition");
echo "</th>";
while ($myrow = biblio_db_fetch_array($result)) 
{
                $nom = $myrow['nom'];
                $tot  = $myrow['tot'];
                $i++;
                if ($i%2 == 1) {$col="WHITE";}
                else {$col="lightyellow";}
                echo "<TR bgcolor=$col><TD>". $nom ."</TD><TD align=right>". $tot ."</TD>";      
	        $pct = sprintf("%2d",($tot / $total_emp) * 100);
                echo "<TD><IMG SRC=\"images/leftbar.gif\" height=\"15\" width=\"7\" Alt=\"$nom : $pct%\" border=0>";
                echo"<IMG SRC=\"images/mainbar.gif\" height=\"15\" width=\"". $pct  ."\" Alt=\"$nom : $pct%\" border=0>";
	        echo "<IMG SRC=\"images/rightbar.gif\" height=\"15\" width=\"7\" Alt=\"$nom : $pct%\" border=0> $pct %</TD>";
}
echo "</TABLE><BR></center>";
?>
<br><br>
<div>
<span id=left-text>
<A HREF="admin.php?menu=1">
<?php echo _("retour menu administration"); ?>
</A><BR>
<A HREF="index.php">
<?php echo _("retour menu principal"); ?>
</A></span>
</div>
<br>
<?php
include("includes/footer.inc");
?>

